//
//  Login.m
//  Wujin
//
//  Created by 东 on 16/12/12.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "Login.h"

@implementation LoginContainer

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.alpha = 0;

}
@end

@interface Login ()

@property (weak, nonatomic) IBOutlet UIButton *phoneNumBtn;


@end

@implementation Login

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _loginBtn.layer.cornerRadius = 3.0;
    _loginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _loginBtn.layer.borderWidth = 1;
    _loginBtn.clipsToBounds = YES;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *phoneNum = [defaults objectForKey:@"phoneStr"];
    
    if (phoneNum) {
        [self.phoneNumBtn setTitle:phoneNum forState:UIControlStateNormal];
    }
    
    
    
    self.phoneNumBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.phoneNumBtn addTarget:self action:@selector(phoneButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)phoneButtonClick {
    
    NSString *phoneStr = self.phoneNumBtn.titleLabel.text;
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phoneStr];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *phone = [defaults objectForKey:@"Phone"];
    NSString *password = [defaults objectForKey:@"Password"];
    
    if ((phone && password) == NO) {
        _txtUser.text = phone;
    }
    
    if (phone && password)
    {
        _txtUser.text = phone;
        _txtPassword.text = password;
        [self Login];
    }
}

#pragma mark  用户登入
/*===================================================================
 说明:
 登入
 ==================================================================*/
- (IBAction)Login
{
    //验证长度
    NSUInteger len = _txtUser.text.length;
    if (len == 0) {[_txtUser becomeFirstResponder]; return;}
    
    len = _txtPassword.text.length;
    if (len == 0) {[_txtPassword becomeFirstResponder]; return;}
    
    //登入用户
    [MRHud show];
    
    [WJUser loginWithPhone:_txtUser.text password:_txtPassword.text result:^(WJUser *user, NSError *error) {
        if (!error)    //登入成功
        {
            [[NSUserDefaults standardUserDefaults] setObject:[FSUser current].username forKey:@"username"];
            [MRHud dismiss];
            [[NSNotificationCenter defaultCenter] postNotificationName:FSUserChangeNotification object:self];
            self.navigationController.navigationBar.hidden = NO;
            [self.view endEditing:YES];
            [self cancel];
        }
        else
        {
            [MRHud dismiss];
            [MRAlert alertWithMessage:error.userInfo[@"msg"]];
        }
    }];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self.view endEditing:YES];
    
}

@end
